﻿using Autofac;
using Autofac.Extensions.DependencyInjection;
using AutoMapper.Configuration;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Moq;
using System.IO;
using VIP.Application.Modules;
using WebApplication;
using Com.Ctrip.Framework.Apollo;
using Com.Ctrip.Framework.Apollo.Logging;

namespace VIP.Tester
{
    public class ContainerHelper
    {



        public static IHttpContextAccessor AddLoginUser(string role = "admin")
        {
            var hca = new Mock<IHttpContextAccessor>();

            var context = new DefaultHttpContext();
            context.Request.Headers.Add(VIPHttpContext.HeaderToken, new Microsoft.Extensions.Primitives.StringValues("05986DDB458B8F10A31D59B613D"));

            hca.Setup(_ => _.HttpContext).Returns(context);


            return hca.Object;
        }

        public static IContainer Builder()
        {

            var Services = new ServiceCollection();
            Services.AddLogging();
            var env = new Mock<IWebHostEnvironment>();
            env.Setup(e => e.EnvironmentName).Returns("Development");
            env.Setup(e => e.ContentRootPath).Returns(Directory.GetCurrentDirectory());





            Microsoft.Extensions.Configuration.IConfiguration cfgConfig = new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile("appsettings.json")
                .Build();

            Microsoft.Extensions.Configuration.IConfiguration cfg = new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddApollo(cfgConfig.GetSection("Apollo"))
                .AddDefault()
                .AddNamespace("VIP.Private", Com.Ctrip.Framework.Apollo.Enums.ConfigFileFormat.Json)
                .Build();

            
            var startup = new Startup(cfg);

            startup.ConfigureServices(Services);

            var builder = new ContainerBuilder();

            builder.Register(ctx => AddLoginUser());

            startup.ConfigureContainer(builder);

            builder.Populate(Services);

            return builder.Build();


        }

    }
}
